package com.workflow.action;

import java.util.List;
import java.util.Set;

import com.base.service.CardTypeService;
import com.base.service.DepartmentService;
import com.base.service.DictService;
import com.base.service.FlowworkitemService;
import com.base.service.PermissionService;
import com.common.bean.Cardmaintype;
import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Flowtemplatedetail;
import com.common.bean.Flowworkitem;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Inputdept;
import com.workflow.service.InputdeptService;

public class InputdeptManagerAction extends AbstractActionSupport implements
		Preparable {
	private static final long serialVersionUID = -7298340303337129915L;

	private InputdeptService inputdeptService;
	
	private DepartmentService departmentService;
	
	private DictService dictService;
	
	private FlowworkitemService flowworkitemService;
	
	private CardTypeService cardTypeService;
	
	private PermissionService permissionService;
	
	private Inputdept inputdept;
	
	private String inputno;
	
	private PagerModel pm;
	
	private List<Department> departmentByDepartmentDeptcodelist;
	
	private List<Datadict> datadictByMarklist;
	
	private List<Datadict> datadictByUnitlist;
	
	private List<Datadict> datadictByPrilist;
	
	private List<Datadict> datadictByStatuslist;
	
	private List<Cardmaintype> cardmaintypelist;
	
	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
		departmentByDepartmentDeptcodelist = departmentService.findAllleafDepartment();
		cardmaintypelist = cardTypeService.findAllCardmaintype();
		datadictByUnitlist = dictService.findDictDetail(StringToken.DICT_WORK_UNIT);
		datadictByPrilist = dictService.findDictDetail(StringToken.DICT_WORK_PRI);
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddInputdept() throws Exception {
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addInputdept() throws Exception {
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		Flowworkitem flowworkitem = null;
		/*
		 admin 账户为超级管理员用户！测试专用。		 
		 */
		String deptcode = "";
		Department department = new Department();
		if("admin".equals(personnel.getLoginname())){
			deptcode = "0027";
			department.setDeptcode(deptcode);
		}else {
			deptcode = personnel.getDepartment().getDeptcode();
			department.setDeptcode(deptcode);
		}
		flowworkitem = flowworkitemService.finaFlowworkitem(deptcode
				, inputdept.getCardmaintype().getCardmaintypeno()/*类型一致*/
				, StringToken.DICT_FLOWFLAG_INPUTDEPT);
		
		inputdept.setPersonnel(personnel);
		inputdept.setDepartmentByDeptcode(department);
		inputdept.setDepartmentByDepartmentDeptcode(personnel.getDepartment());//同一个部门间入库
		inputdept.setFlowworkitem(flowworkitem);
		
		Datadict datadictByMark = new Datadict();
		datadictByMark.setTypeid(StringToken.DICT_WORK_MARK_NO);
		inputdept.setDatadictByMark(datadictByMark);
		
		Object obj = null;
		Set<Flowtemplatedetail>	set = flowworkitem.getFlowtemplatedetails();
		for(Flowtemplatedetail flowtemplatedetail : set) {
			if(flowtemplatedetail.getSeq() == 1l) {
				//判断用户权限部门、角色
				if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
					long ruleno = flowtemplatedetail.getRule().getRuleno();
					String staffcode = personnel.getStaffcode();
					
					obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
				}
				
				inputdept.setFlowtemplatedetail(flowtemplatedetail);//默认1为保存，从1开始
				inputdept.setDatadictByStatus(flowtemplatedetail.getDatadictByWorkflowstatusno());//保存状态,默认
				inputdept.setDatadictByBranch(flowtemplatedetail.getDatadictByBranch());//是否分支 :流程1时，默认取1分支
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		inputdeptService.addInputdept(inputdept);
		return SUCCESS;
	}
	
	public String findAllInputdept() throws Exception {
		pm = inputdeptService.findInputdeptForPager();
		return SUCCESS;
	}
	
	public String findInputdept() throws Exception {
		inputdept = inputdeptService.findInputdept(inputno);
		return SUCCESS;
	}
	
	public String delInputdept() throws Exception {
		inputdeptService.removeInputdept(inputno);
		return SUCCESS;
	}
	
	public String getInputdeptForUpdate() throws Exception {
		inputdept = inputdeptService.findInputdept(inputno);
		return SUCCESS;
	}
	
	public String updateInputdept() throws Exception {
		Inputdept input = null;
		input = inputdeptService.findInputdept(inputdept.getInputno());
		
		if(input == null ) {
			throw new RuntimeException("has found no instance ");
		}
		input.setDatadictByUnit(inputdept.getDatadictByUnit());
		input.setDatadictByPri(inputdept.getDatadictByPri());
		input.setLimitday(inputdept.getLimitday());
		input.setRemark(inputdept.getRemark());
		inputdeptService.updateInputdept(input);
		return SUCCESS;
	}

	public List<Cardmaintype> getCardmaintypelist() {
		return cardmaintypelist;
	}

	public void setCardmaintypelist(List<Cardmaintype> cardmaintypelist) {
		this.cardmaintypelist = cardmaintypelist;
	}

	public CardTypeService getCardTypeService() {
		return cardTypeService;
	}

	public void setCardTypeService(CardTypeService cardTypeService) {
		this.cardTypeService = cardTypeService;
	}

	public List<Datadict> getDatadictByMarklist() {
		return datadictByMarklist;
	}

	public void setDatadictByMarklist(List<Datadict> datadictByMarklist) {
		this.datadictByMarklist = datadictByMarklist;
	}

	public List<Datadict> getDatadictByPrilist() {
		return datadictByPrilist;
	}

	public void setDatadictByPrilist(List<Datadict> datadictByPrilist) {
		this.datadictByPrilist = datadictByPrilist;
	}

	public List<Datadict> getDatadictByStatuslist() {
		return datadictByStatuslist;
	}

	public void setDatadictByStatuslist(List<Datadict> datadictByStatuslist) {
		this.datadictByStatuslist = datadictByStatuslist;
	}

	public List<Datadict> getDatadictByUnitlist() {
		return datadictByUnitlist;
	}

	public void setDatadictByUnitlist(List<Datadict> datadictByUnitlist) {
		this.datadictByUnitlist = datadictByUnitlist;
	}

	public List<Department> getDepartmentByDepartmentDeptcodelist() {
		return departmentByDepartmentDeptcodelist;
	}

	public void setDepartmentByDepartmentDeptcodelist(
			List<Department> departmentByDepartmentDeptcodelist) {
		this.departmentByDepartmentDeptcodelist = departmentByDepartmentDeptcodelist;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public DictService getDictService() {
		return dictService;
	}

	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	public FlowworkitemService getFlowworkitemService() {
		return flowworkitemService;
	}

	public void setFlowworkitemService(FlowworkitemService flowworkitemService) {
		this.flowworkitemService = flowworkitemService;
	}

	public Inputdept getInputdept() {
		return inputdept;
	}

	public void setInputdept(Inputdept inputdept) {
		this.inputdept = inputdept;
	}

	public InputdeptService getInputdeptService() {
		return inputdeptService;
	}

	public void setInputdeptService(InputdeptService inputdeptService) {
		this.inputdeptService = inputdeptService;
	}

	public String getInputno() {
		return inputno;
	}

	public void setInputno(String inputno) {
		this.inputno = inputno;
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public PagerModel getPm() {
		return pm;
	}

	public void setPm(PagerModel pm) {
		this.pm = pm;
	}
	
}
